<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>(点击重新设计)工序物料</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css?v=4.1.0" rel="stylesheet">
    <link href="/bootstraptable/bootstrap-table.css" rel="stylesheet">
</head>
<body>
<div style="color: #0e9aef;margin: 20px;">您正在做的业务是：生产管理--工序物料设计--制定工序物料设计单</div>

<!--查询工序物料明细的div-->
<div class="container" id="operation_material_redesign_div">
    <div id="operation_material_redesign_div_but">
        <span style="color: red">重新设计将首先清除原有本工序物料的设计，您确认进行重新设计吗？</span>
        <button type="button" class="btn btn-default col-md-offset-6" id="ok_btn">确认</button>
        <button type="button" class="btn btn-default" onclick="history.back()">返回</button>
    </div>
    <div class="panel panel-default" style="margin-top: 10px;">
        <div class="page-heading">
            <div class="panel-title" style="margin-top: 20px;">
                <h2 style="text-align: center;">工序物料设计单</h2>
            </div>
        </div>
        <div class="panel-body">
            <form class="form-horizontal">
            <div class="form-group">
                <div class="col-md-6">
                    <label class="control-label col-md-4" style="margin-right: 0px;">工序单编号：</label>
                    <div class="col-md-8">
                        <input type="text" class="form-control" style="border: none; background-color: white;" readonly
                               name="designId" id="designId">
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="control-label col-md-4">工序名称：</label>
                    <div class="col-md-4">
                        <input type="text" class="form-control" style="border: none; background-color: white;" readonly
                               name="procedureName" id="procedureName">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-6">
                    <label class="control-label col-md-4">登记人：</label>
                    <div class="col-md-8">
                        <input type="text" class="form-control" style="border: none;" name="register" id="register">
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="control-label col-md-4">登记时间：</label>
                    <div class="col-md-8">
                        <input type="text" class="form-control"
                               style="border: none; background-color: white;"
                               eadonly name="registerTime" id="registerTime">
                    </div>
                </div>
            </div>
            <!--     工序物料明细表格信息       -->
            <table class="table table-bordered table-striped" data-toggle="table"
                   id="operation_material_redesign_table">
                <thead>
                <tr>
                    <th data-field="detailsNumber">序号</th>
                    <th data-field="productName">物料名称</th>
                    <th data-field="productId">物料编号</th>
                    <th data-field="productDescribe">描述</th>
                    <th data-field="type" data-formatter="formatterType">用途类型</th>
                    <th data-field="amount">本工序数量</th>
                    <th data-field="amountUnit">单位</th>
                    <th data-field="costPrice">单价（元）</th>
                    <th data-field="subtotal">小计（元）</th>
                </tr>
                </thead>
            </table>
            <input type="hidden" name="procedureId" id="procedureId">,
            </form>
        </div>
    </div>
</div>
.

<!--点击确定删除数据后 重新设计工序物料-->
<div class="container" id="redesign_process_material_div" style="display: none;">
    <div style="text-align: right">
        <button type="button" name="" class="btn btn-default" id="preview_btn">预览</button>
        <button type="button" class="btn btn-default" onclick="history.back()">返回</button>
    </div>
    <div class="panel panel-default">
        <div class="page-heading">
            <div class="panel-title">
                <h2 style="text-align: center;margin-top: 20px;">工序物料设计单</h2>
            </div>
        </div>
        <div class="panel-body">
            制造：<span id="changpingName"></span>产品，供选择的物料清单如下：
            <table class="table table-bordered table-striped" data-toggle="table" id="redesign_process_material_table">
                <thead>
                <th data-field="detailsNumber">序号</th>
                <th data-field="productName">物料名称</th>
                <th data-field="productId">物料编号</th>
                <th data-field="productDescribe">描述</th>
                <th data-field="amount">设计数量</th>
                <th data-field="residualAmount">可用数量</th>
                <th data-field="amountUnit">单位</th>
                <th data-field="costPrice">单价（元）</th>
                <th data-field="amount" data-formatter="formatterAmount">本工序数量</th>
                </thead>
            </table>
        </div>
    </div>
</div>

<!--点击预览按钮后-->
<div class="container" style="display: none;" id="preview_redesign_process_material_div">
    <div style="text-align: right;">
        <button type="button" id="sure_btn" class="btn btn-default">确定</button>
        <button type="button" class="btn btn-default" onclick="history.back()">返回</button>
    </div>
    <div class="panel panel-default" style="margin-top: 10px;">
        <div class="page-heading">
            <div class="panel-title" style="margin-top: 20px;">
                <h3 style="text-align: center;">工序物料设计单</h3>
            </div>
        </div>
        <div class="panel-body">
            <div class="form-group">
                <div class="col-md-6">
                    <label class="control-label col-md-4">工序单编号：</label>
                    <div class="col-md-8">
                        <input type="text" style="border: none; background-color: white;" readonly name="designId"
                               id="dId">
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="control-label col-md-4">工序名称：</label>
                    <div class="col-md-8">
                        <input type="text" style="border: none; background-color: white;" readonly name="procedureName"
                               id="pName">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-6">
                    <label class="control-label col-md-4">登记人：</label>
                    <div class="col-md-8">
                        <input type="text" style="border: none;border-bottom: 1px solid black;" name="register"
                               id="reg">
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="control-label col-md-4">登记时间：</label>
                    <div class="col-md-8">
                        <input type="text" style="border: none; background-color: white;" readonly name="registerTime"
                               id="regiTime">
                    </div>
                </div>
            </div>
            <!--点击预览按钮后-->
            <table class="table table-bordered table-striped" data-toggle="table"
                   id="preview_redesign_process_material_table">
                <thead>
                <tr>
                    <th data-field="detailsNumber">序号</th>
                    <th data-field="productName">物料名称</th>
                    <th data-field="productId">物料编号</th>
                    <th data-field="productDescribe">描述</th>
                    <th data-field="amount">本工序数量</th>
                    <th data-field="amountUnit">单位</th>
                    <th data-field="costPrice">单价（元）</th>
                    <th data-field="subtotal">小计（元）</th>
                </tr>
                </thead>
            </table>
            <input type="hidden" name="gxId" id="gxId">
        </div>
    </div>
</div>

<input type="hidden" id="bb">
<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4"></script>
<script src="/js/bootstrap.min.js?v=3.3.6"></script>
<script src="/js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="/js/plugins/layer/layer.min.js"></script>

<!-- 自定义js -->
<script src="/js/hAdmin.js?v=4.1.0"></script>
<script src="/js/currentTime.js"></script>

<!-- 第三方插件 -->
<script src="/js/plugins/pace/pace.min.js"></script>

<!--BootstrapTable-->
<script src="/bootstraptable/bootstrap-table.min.js"></script>
<script src="/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>

<script>
    $(function () {
        //获取工序id
        let redesignId = localStorage.getItem("redesignId")
        //产品编号
        let productId = localStorage.getItem("productId")

        $.post("/manufacture/process/getProductIdQueryId", {"id": productId}, function (res) {
            $("#operation_material_redesign_div #register").val(res.register)
            $("#operation_material_redesign_div #designId").val(res.designId)
            $("#operation_material_redesign_div #registerTime").val(formater(res.registerTime))
        })
        $.post("/manufacture/process/getIdQueryProcess", {"id": redesignId}, function (res) {
            $("#operation_material_redesign_div #procedureName").val(res.procedureName)
            $("#operation_material_redesign_div #procedureId").val(res.procedureId)
        })
        setTimeout(function () {
            //查询工序物料明细
            $.post("/manufacture/process/operationItemInquiry", {
                "productId": productId,
                "procedureId": $("#procedureId").val()
            }, function (res) {
                for (let i = 0; i < res.length; i++) {
                    $("#operation_material_redesign_div #operation_material_redesign_table").bootstrapTable("append", res[i])
                }
            })
        }, 100)

        //点击确定按钮，删除工序物料明细信息，以及修改可用数量，设计标志位
        $("#operation_material_redesign_div_but #ok_btn").click(function () {
            //获取工序物料明细表格的数据
            let data = $("#operation_material_redesign_div #operation_material_redesign_table").bootstrapTable("getData")

            let dto = {
                "productId": localStorage.getItem("productId"),
                "procedureId": $("#procedureId").val()
            }
            dto.modules = data
            //发送请求
            $.ajax({
                url: "/manufacture/process/deleteOperationItemDetails",
                type: "post",
                data: JSON.stringify(dto),
                contentType: "application/json",
                success: function (data) {
                    if (data) {
                        //隐藏查询的页面
                        $("#operation_material_redesign_div").hide()
                        $("#changpingName").html(localStorage.getItem("name"))
                        $("#redesign_process_material_div").show()
                        $.post("/manufacture/process/getModuleDetails", {"productId": productId}, function (res) {
                            for (let i = 0; i < res.length; i++) {
                                $("#redesign_process_material_table").bootstrapTable("append", res[i])
                            }
                        })
                    }
                }
            })
        })

        //点击预览按钮
        $("#redesign_process_material_div #preview_btn").click(function () {
            if($("#bb").val()=="本工序数量不能超过可用数量"){
                let d= $("#preview_redesign_process_material_table").bootstrapTable("getData")
                console.log(d)
                for (let i=0;i<d.length;i++){
                    d.splice(i,1)
                    d.splice(i,1)
                }
            }
            //获取重新设计的表格数据
            let data = $("#redesign_process_material_div #redesign_process_material_table").bootstrapTable("getData")
            console.log("预览前的表格数据",data)
            //标记变量 用来判断输入数量是否大于可用数量
            let flag = -1
            for (let i = 0; i < data.length; i++) {
                if (data[i].amount > data[i].residualAmount) {
                    $("#bb").val("本工序数量不能超过可用数量")
                    alert("本工序数量不能超过可用数量")
                    flag = i
                    return
                }
                data[i].subtotal = parseInt(data[i].costPrice) * data[i].amount
                $("#preview_redesign_process_material_div #preview_redesign_process_material_table").bootstrapTable("append", data[i])
            }
            //获取预览后表格加载的数据
            let data1 = $("#preview_redesign_process_material_div #preview_redesign_process_material_table").bootstrapTable("getData")


            let text = $("#preview_redesign_process_material_table tbody ").find("tr")
            for (let i = text.length - 1; i >= 0; i--) {
                //拿到数量
                let x = $(text[i]).find("td").eq(4).html()
                if (x == "0") {
                    data1.splice(i, 1)
                    $(text[i]).remove()
                }
            }
            if (flag == -1) {
                $("#redesign_process_material_div").hide()
                $("#preview_redesign_process_material_div").show()

                $.post("/manufacture/process/getProductIdQueryId", {"id": productId}, function (res) {
                    $("#preview_redesign_process_material_div #reg").val(res.register)
                    $("#preview_redesign_process_material_div #dId").val(res.designId)
                    $("#preview_redesign_process_material_div #regiTime").val(formater(res.registerTime))
                })
                $.post("/manufacture/process/getIdQueryProcess", {"id": redesignId}, function (res) {
                    $("#preview_redesign_process_material_div #pName").val(res.procedureName)
                    $("#preview_redesign_process_material_div #gxId").val(res.id)
                })
            }
        })

        //点击确定按钮
        $("#preview_redesign_process_material_div #sure_btn").click(function (){
            //获取表格数据
            let data=$("#preview_redesign_process_material_div #preview_redesign_process_material_table").bootstrapTable("getData")
            for (let i=0;i<data.length;i++){
                if(data[i].amount==0){
                    data[i].remove
                }
            }
            let dto={
                "productId":localStorage.getItem("productId"),
                "id":$("#preview_redesign_process_material_div #gxId").val()
            }
            dto.details=data
            //发送请求
            $.ajax({
                url:"/manufacture/process/addOperationMaterial",
                type: "post",
                data: JSON.stringify(dto),
                contentType: "application/json",
                success: function (data) {
                    if(data){
                        localStorage.setItem("id",localStorage.getItem("id"))
                        window.location="manufacture_make_operation_materialUI"
                    }
                }
            })
        })


    })

    //格式化类型
    function formatterType(value, row, index) {
        return value == "Y001-2" ? "物料" : "商品"
    }

    //格式化本工序数量文本框
    function formatterAmount(value, row, index) {
        return '<input type="text" name="amount" onblur="changeAmounts(this)" />'
    }

    //本工序数量失去焦点时
    function changeAmounts(parms) {
        // 文本框的数量
        let val = $(parms).val()
        // 获取文本框所在的行索引
        let index = $(parms).parent("td").parent().index()

        // 获取物料明细表格的所有数据
        let data = $("#redesign_process_material_div #redesign_process_material_table").bootstrapTable("getData")
        // 修改对应对象的amount
        data[index].amount = parseInt(val)
    }
</script>

</body>
</html>